package gui;


import gui.components.CalcTextField;

import java.awt.EventQueue;

import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import core.TextFieldModel;
import core.listeners.OperationListener;
import core.listeners.TextChangeListener;

import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWindow {

	private JFrame frame;
	private CalcTextField textField;
	private JButton buttonMR;
	private JButton button9;
	private JButton buttonDivide;
	private JButton buttonSQRT;
	private JButton button4;
	private JButton buttonCE;
	private JButton button8;
	private JButton button5;
	private JButton button6;
	private JButton buttonMultiply;
	private JButton button7;
	private JButton buttonPow;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton buttonM;
	private JButton buttonMC;
	private JButton buttonSubtract;
	private JButton buttonInverse;
	private JButton buttonSign;
	private JButton buttonPI;
	private JButton button0;
	private JButton buttonComma;
	private JButton buttonAdd;
	private JButton buttonEqual;
	
	private OperationListener operationListener;
	private TextChangeListener textChangeListener;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
		addEventListeners();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 300, 350);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout());
		
		textField = new CalcTextField("0");
		textField.setEditable(false);
		textField.setPreferredSize(new Dimension(6, 50));
		frame.getContentPane().add(textField, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(5, 5, 0, 0));
		
		buttonMR = new JButton("MR");
		panel.add(buttonMR);
		
		buttonM = new JButton("M+");
		panel.add(buttonM);
		
		buttonMC = new JButton("MC");
		panel.add(buttonMC);
		
		buttonCE = new JButton("CE");
		panel.add(buttonCE);
		
		buttonPI = new JButton("Pi");
		panel.add(buttonPI);
		
		button7 = new JButton("7");
		panel.add(button7);
		
		button8 = new JButton("8");
		panel.add(button8);
		
		button9 = new JButton("9");
		panel.add(button9);
		
		buttonDivide = new JButton("/");
		panel.add(buttonDivide);
		
		buttonSQRT = new JButton("sqrt");
		panel.add(buttonSQRT);
		
		button4 = new JButton("4");
		panel.add(button4);
		
		button5 = new JButton("5");
		panel.add(button5);
		
		button6 = new JButton("6");
		panel.add(button6);
		
		buttonMultiply = new JButton("*");
		panel.add(buttonMultiply);
		
		buttonPow = new JButton("pow");
		panel.add(buttonPow);
		
		button1 = new JButton("1");
		panel.add(button1);
		
		button2 = new JButton("2");
		panel.add(button2);
		
		button3 = new JButton("3");
		panel.add(button3);
		
		buttonSubtract = new JButton("-");
		panel.add(buttonSubtract);
		
		buttonInverse = new JButton("1/x");
		panel.add(buttonInverse);
		
		buttonSign = new JButton("+/-");
		panel.add(buttonSign);
		
		button0 = new JButton("0");
		panel.add(button0);
		
		buttonComma = new JButton(".");
		panel.add(buttonComma);
		
		buttonAdd = new JButton("+");
		panel.add(buttonAdd);
		
		buttonEqual = new JButton("=");
		panel.add(buttonEqual);
		
	}
	
	public void addEventListeners()
	{
		textChangeListener = new TextChangeListener(textField);
		operationListener = new OperationListener(textField);
		
		buttonPI.addActionListener(textChangeListener);
		button0.addActionListener(textChangeListener);
		button1.addActionListener(textChangeListener);
		button2.addActionListener(textChangeListener);
		button3.addActionListener(textChangeListener);
		button4.addActionListener(textChangeListener);
		button5.addActionListener(textChangeListener);
		button6.addActionListener(textChangeListener);
		button7.addActionListener(textChangeListener);
		button8.addActionListener(textChangeListener);
		button9.addActionListener(textChangeListener);
		buttonComma.addActionListener(textChangeListener);
		buttonSign.addActionListener(textChangeListener);
		
		buttonCE.addActionListener(operationListener);
		buttonAdd.addActionListener(operationListener);
		/*buttonSubtract.addActionListener(new OperationListener(textField));
		buttonDivide.addActionListener(new OperationListener(textField));
		buttonMultiply.addActionListener(new OperationListener(textField));
		buttonSQRT.addActionListener(new OperationListener(textField));
		buttonPow.addActionListener(new OperationListener(textField));*/
		buttonEqual.addActionListener(operationListener);

	}

}
